import { Component, Emit, Prop, Watch } from 'vue-facing-decorator'
import Render from '@/render/Render'
import toNative from '@/utils/toNative'
import type { FormModalDto } from '@/components/modal/Modal'
import renderModal, { getModelValue, initModelValue } from '@/components/modal/Modal'
import type { AnyObject } from 'ant-design-vue/es/_util/type'
import type { InvoiceReqDto } from '@/api/invoice/types'
import { InvoiceUploadStatusCnEnum } from '@/const/enum'
import { exportInvoice } from '@/api/invoice'
import { downloadFile } from '@/utils/util'

@Component
export class DialogExportApplyCompany extends Render {
    @Prop() show!: boolean
    @Prop() params!: InvoiceReqDto

    @Emit('update:show')
    updateShow(val: boolean) {
        return val
    }

    @Watch('show')
    handleWatchShow() {
        console.log(8888, this.show)
        this.formConfig.show = this.show
    }

    formConfig = this.initFormConfig() as FormModalDto
    initFormConfig() {
        return {
            title: '导出申请企业信息',
            width: '400px',
            theme: 'light',
            show: false,
            items: {
                applicationMonth:{
                    name: '申请月份',
                    element: 'datetime',
                    required: true,
                    params: {
                        type: 'month',
                        valueFormat: 'YYYY-MM'
                    }
                },
            },
            complete: async () => {
                const data = getModelValue(this.formConfig)
                const params = Object.assign(this.params, data)
                params.status = InvoiceUploadStatusCnEnum.已上传
                const res = await exportInvoice(params)
                downloadFile(res)
                // this.formConfig.cancel()
            },
            cancel: () => {
                this.formConfig.show = false
                this.updateShow(false)
            },
            init: () => {
                initModelValue(this.formConfig, this.params)
            }
        } as FormModalDto
    }

    render() {
        return <>
            {renderModal(this.formConfig)}
        </>
    }

}

export default toNative<{params: InvoiceReqDto}, {}>(DialogExportApplyCompany)